﻿using SIG.Basic.Extensions;
using SIG.Data.Repositories.Case;
using SIG.Infrastructure.Cache;
using SIG.Infrastructure.Configs;
using SIG.Infrastructure.Logging;
using SIG.Model.Case;
using SIG.Resources.Admin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;

namespace SIG.Service.Case
{
    public class WorkTypeService : EntityService<WorkType>, IWorkTypeService
    {

        private readonly IWorkTypeRepository _workTypeRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public WorkTypeService(IWorkTypeRepository workTypeRepository, ILoggingService loggingService, ICacheService cacheService)
            : base(workTypeRepository, cacheService, loggingService)
        {
            _workTypeRepository = workTypeRepository;
            _loggingService = loggingService;
            _cacheService = cacheService;
        }


        public async Task<List<WorkType>> GetActiveElementsAync(CancellationToken cancellationToken = default(CancellationToken))
        {
            //get list
            var key = $"{EntityNames.WorkCategory}s_List_Active";

            List<WorkType> workTypelist;
            if (SettingsManager.Case.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    workTypelist = (List<WorkType>)_cacheService.Get(key);

                }
                else
                {
                    var workTypes = await _workTypeRepository.FindByAsync(m => m.Active, m => m.Importance, false, cancellationToken);
                    workTypelist = workTypes.ToList();
                    _cacheService.Set(key, workTypelist, SettingsManager.Case.CacheDuration);
                }
            }
            else
            {
                var workTypes = await _workTypeRepository.FindByAsync(m => m.Active, m => m.Importance, false, cancellationToken);
                workTypelist = workTypes.ToList();
            }

            return workTypelist;

        }

        public WorkType GetByIdWithWorks(int id)
        {

            var key = $"{EntityNames.WorkCategory}_ByIdWithWorks_{id}";
            if (!SettingsManager.Client.EnableCaching)
            {
                return _workTypeRepository.GetFirstOrDefault(p => p.Id == id, p => p.Works);
            }

            if (_cacheService.IsSet(key))
            {
                return (WorkType)_cacheService.Get(key);

            }

            var workType = _workTypeRepository.GetFirstOrDefault(p => p.Id == id, p => p.Works);
            _cacheService.Set(key, workType, SettingsManager.Client.CacheDuration);

            return workType;
        }

        public List<WorkType> GetPagedElements(int pageIndex, int pageSize, string keyword, out int totalCount)
        {

            //get list count
            var keyCount = $"{EntityNames.WorkCategory}_ListCount_{keyword}";

            var totalIQuery = _workTypeRepository.GetAllElements().Where(g => g.Active);
            if (!string.IsNullOrEmpty(keyword))
                totalIQuery = totalIQuery.Where(g => g.Title.Contains(keyword));

            if (SettingsManager.Case.EnableCaching)
            {
                if (_cacheService.IsSet(keyCount))
                {
                    totalCount = (int)_cacheService.Get(keyCount);
                }
                else
                {
                    totalCount = totalIQuery.Count();
                    _cacheService.Set(keyCount, totalCount, SettingsManager.Case.CacheDuration);
                }
            }
            else
            {
                totalCount = totalIQuery.Count();
            }


            //get list
            var key = $"{EntityNames.WorkCategory}_List_{pageIndex}_{pageSize}_{keyword}";

            List<WorkType> categories;
            Expression<Func<WorkType, bool>> filter = g => g.Active;
            Expression<Func<WorkType, bool>> filterByKeyword = g => g.Title.Contains(keyword);


            if (!string.IsNullOrEmpty(keyword))
                filter = filter.AndAlso(filterByKeyword);


            if (SettingsManager.Case.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    categories = (List<WorkType>)_cacheService.Get(key);

                }
                else
                {
                    categories = _workTypeRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), filter, false).ToList();
                    _cacheService.Set(key, categories, SettingsManager.Case.CacheDuration);
                }
            }
            else
            {
                categories = _workTypeRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), filter, false).ToList();
            }

            return categories;

        }

    }
}
